package com.iflytek.view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import priv.mill.net.MyHttp;

import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;

public class mk2 {

	/***
	 * 这里需要改成你自己的实际appid
	 */
	public SpeechRecognizer recognizer;
	String resultString;
	StringBuilder stringBuilder = new StringBuilder();
	public StringBuilder finalString;

	/***
	 * 初始化声音组件
	 */

	public void listen() {

		SpeechUtility.createUtility(SpeechConstant.APPID + "=59bc97bb");//
		recognizer = SpeechRecognizer.createRecognizer();
		recognizer = SpeechRecognizer.getRecognizer();
		recognizer.setParameter(SpeechConstant.DOMAIN, "iat"); // domain:域名
		recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
		recognizer.setParameter(SpeechConstant.ACCENT, "mandarin");
		recognizer.setParameter(SpeechConstant.ASR_PTT, "1");// 是否带标点符号
		recognizer.setParameter(SpeechConstant.VAD_EOS, "700");// 静音700毫秒结束录音

		// 开始监听语音输入
		recognizer.startListening(new RecognizerListener() {

			@Override
			public void onVolumeChanged(int arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(RecognizerResult result, boolean arg1) {

				resultString = result.getResultString();
				finalString = finalString(resultString);
				// System.out.println(finalString);

			}

			@Override
			public void onEvent(int arg0, int arg1, int arg2, String arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(SpeechError arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onEndOfSpeech() {
				System.out.println("结束录音");
			}

			@Override
			public void onBeginOfSpeech() {
				System.out.println("开始录音");
			}
		});

	}

	public StringBuilder Ting() {
		listen();
		return finalString;

	}

	/*
	 * 正则匹配 合成字符串
	 */
	public StringBuilder finalString(String string) {

		Pattern compile = Pattern.compile("\"w\":\"([\\W\\w]{1,5})\"");
		Matcher matcher = compile.matcher(string);
		while (matcher.find()) {
			stringBuilder.append(matcher.group(1));

		}
		return stringBuilder;
	}
}
